//
//  DVPhotoCell.m
//  ImagePicker
//
//  Created by csh on 16/4/13.
//  Copyright © 2016年 com.read. All rights reserved.
//
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "DVPhotoCell.h"

@interface DVPhotoCell ()
@property (nonatomic,strong)UIImageView *photo;
@end

@implementation DVPhotoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self initSubview];
    }
    
    return self;
}
- (void)compareWithAsset:(NSArray *)assets;
{
    BOOL isExist = NO;
    for(DVAssetModel *model in assets)
    {
        
        if([model.asset isKindOfClass:[PHAsset class]])
        {
            PHAsset *asset1 = model.asset;
            PHAsset *asset2 = self.model.asset;
            if([asset1.creationDate isEqual:asset2.creationDate])
            {
                self.selectButton.selected = YES;
                isExist = YES;
            }
        }else
        {
            ALAsset *asset1 = model.asset;
            ALAsset *asser2 = self.model.asset;
            if([[asset1 valueForProperty:ALAssetPropertyDate] isEqual:[asser2 valueForProperty:ALAssetPropertyDate]])
            {
                self.selectButton.selected = YES;
                isExist = YES;
            }
        }
            
    }
    if(!isExist)
    {
        self.selectButton.selected = NO;
    }
}

- (void)setModel:(DVAssetModel *)model
{
    _model = model;
    [[DVImagePickerManager manager] getPhotoWithAsset:model.asset imageWidth:self.frame.size.width Widthcompletion:^(UIImage *image) {
//        NSData *fData = UIImageJPEGRepresentation(image, 0.1);
//        UIImage *img=[UIImage imageWithData:fData];
        self.photo.image = image;
    }];
}


#pragma - mark initSubview
- (void)initSubview
{
    self.photo = [[UIImageView alloc]init];
    self.photo.contentMode = UIViewContentModeScaleAspectFill;
    self.photo.clipsToBounds = YES;
    self.photo.layer.masksToBounds = YES;
    self.photo.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.photo];
    NSMutableArray *photoConstraint = [[NSMutableArray alloc]init];
    [photoConstraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[photo]-0-|" options:0 metrics:nil views:@{@"photo":self.photo}]];
    [photoConstraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[photo]-0-|" options:0 metrics:nil views:@{@"photo":self.photo}]];
    [self.contentView addConstraints:photoConstraint];
    
    self.selectButton = [[DVSelectButton alloc]init];
    self.selectButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.selectButton setImage:[UIImage imageNamed:@"image_normal"] forState:UIControlStateNormal];
    [self.selectButton addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.selectButton setImage:[UIImage imageNamed:@"image_select"] forState:UIControlStateSelected];
    [self.contentView addSubview:self.selectButton];
    NSMutableArray *buttonConstraint = [[NSMutableArray alloc]init];
    [buttonConstraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[selectButton(20)]-(0)-|" options:0 metrics:nil views:@{@"selectButton":self.selectButton}]];
    [buttonConstraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[selectButton(20)]-(>=0)-|" options:0 metrics:nil views:@{@"selectButton":self.selectButton}]];
    [self.contentView addConstraints:buttonConstraint];
}


#pragma - mark ButtonClick
- (void)selectButtonClick:(DVSelectButton *)button
{
    if(button.selected == YES)
    {
        button.selected = NO;
        self.model.select = NO;
    }else
    {
        button.selected = YES;
        self.model.select = YES;
    }
    if([self.delegate respondsToSelector:@selector(didSelectImageWithModel:select:)])
    {
        [self.delegate didSelectImageWithModel:self.model select:button.selected];
    }
}

@end

@implementation DVSelectButton

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    return contentRect;
}

@end
